package com.joey.cloud.flowable.listener;

import com.alibaba.fastjson.JSONObject;
import com.joey.cloud.flowable.event.MyFlowMsgEvent;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * flow全局事件监听
 */
@Component
@Slf4j
public class MyGlobalFlowableEventListener implements FlowableEventListener {
    @Resource
    ApplicationContext context;

    @Override
    public void onEvent(FlowableEvent event) {
        String eventType = event.getType().name();
        JSONObject eventJson = JSONObject.parseObject(JSONObject.toJSONString(event));
        //触发自定义事件
        context.publishEvent(new MyFlowMsgEvent(this,eventType,eventJson));
    }
    /**
     * onEvent(..)方法在调度事件时抛出异常时的行为
     * false=异常被忽略,true=异常不会被忽略
     * @return
     */
    @Override
    public boolean isFailOnException() {
        return false;
    }
    /**
     * 确定此事件侦听器是在事件发生时立即触发还是由getOnTransaction()方法确定的事务生命周期事件触发。
     * 支持的事务生命周期事件的值是：COMMITTED，ROLLED_BACK，COMMITTING，ROLLINGBACK。
     * @return
     */
    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return false;
    }

    @Override
    public String getOnTransaction() {
        return null;
    }
}
